package nyist.media.controller;

import nyist.media.annotation.Log;
import nyist.media.enums.RepairStatus;
import nyist.media.page.ApiResult;
import nyist.media.service.RepairOrdersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 维修单
 *
 * @author dcf
 * @date 2019/3/19
 */

@RestController
@RequestMapping(value = "/admin/repair_records", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
@ResponseBody
public class RepairOrdersController extends BaseController {

    @Autowired
    private RepairOrdersService repairOrdersService;

    @GetMapping
    public Map<String, Object> repairOrders(Integer page, Integer limit, RepairStatus status) {
        PageRequest pageRequest = new PageRequest(page - 1, limit);
        return toResultPage(repairOrdersService.findPageByStatus(pageRequest, status));
    }

    /**
     * 驳回维修单
     *
     * @param id 维修单ID
     * @return 响应成功状态码和状态信息
     */
    @PostMapping("/{id}/rejected")
    public Map<String, Object> rejectedOrders(@PathVariable Long id, String rejectedReason) {
        repairOrdersService.rejectedOrder(id, rejectedReason);
        return ApiResult.SUCCESS.getMap();
    }

    /**
     * 提交维修单
     *
     * @param id  教室ID
     * @param txt 问题描述
     * @return 状态码：20000 状态信息：响应成功
     */
    @Log(value = "提交维修单")
    @PostMapping("/{id}/save")
    public Map<String, Object> commitRepairRecord(@PathVariable Long id, String txt) {
        repairOrdersService.createRepairOrders(id, txt, getCurrentInfo());
        return ApiResult.SUCCESS.getMap();
    }


    /**
     * 删除维修单
     * @param id 维修单ID
     * @return 状态码 20000 状态信息：响应成功
     */
    @Log(value = "删除维修单")
    @DeleteMapping("/{id}")
    public Map<String, Object> deleteOrders(@PathVariable Long id) {
        repairOrdersService.deleteById(id);
        return ApiResult.SUCCESS.getMap();
    }

    /**
     * 查看我的维修单
     * @param page 页码
     * @param limit 页长
     * @return 我的维修单列表
     */
    @GetMapping("/my")
    public Map<String, Object> myOrders(Integer page, Integer limit) {
        PageRequest pageRequest = new PageRequest(page - 1, limit);
        return toResultPage(repairOrdersService.findMyPage(pageRequest, getCurrentInfo()));
    }

    @Log(value = "标记维修单完成")
    @PutMapping("/{id}/finished")
    public Map<String, Object> finishedOrders(@PathVariable Long id) {
        repairOrdersService.finishedOrders(id, getCurrentInfo());
        return ApiResult.SUCCESS.getMap();
    }

    @Log(value = "分派维修单")
    @PutMapping("/{id}/deliver")
    public Map<String, Object> deliverRepairMan(@PathVariable Long id, Long repairId) {
        repairOrdersService.deliverOrders(id, repairId);
        return ApiResult.SUCCESS.getMap();
    }

    @GetMapping("/{id}")
    public Map<String, Object> detail(@PathVariable Long id) {
        return ApiResult.SUCCESS.getMap().add("result", repairOrdersService.findOne(id));
    }

}
